<template>
	<use-box :loading="formLoading">
		<el-form :model="form" ref="ruleForm">
			<h3>订单参数</h3>
			<el-form-item class="margin_t_20">
				<div class="dflex_vertical_c">
					<div class="front_txt margin_r_10">商品库存</div>
					<el-radio-group :disabled="addBtnLoading" v-model="form.stock_ded_type">
						<el-radio :label="1">下单后扣减库存</el-radio>
						<el-radio :label="2">支付后扣减库存</el-radio>
					</el-radio-group>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">下单后，超过</div>
					<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
						v-model.number="form.overdue_minute_cancel_order" type="number"
						oninput="value = parseInt(value) || 1"></el-input>
					<div class="rear_txt">分钟未付款，自动取消订单。</div>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">付款后，预计</div>
					<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
						v-model.number="form.predict_day_deliver" type="number" oninput="value = parseInt(value) || 1">
					</el-input>
					<div class="rear_txt">天发货。</div>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">发货后，超过</div>
					<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
						v-model.number="form.overdue_day_receiving" type="number"
						oninput="value = parseInt(value) || 1"></el-input>
					<div class="rear_txt">天，自动确认收货。</div>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">收货后，超过</div>
					<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
						v-model.number="form.overdue_day_close_evaluated" type="number"
						oninput="value = parseInt(value) || 1"></el-input>
					<div class="rear_txt">天，关闭评价订单改为已完成。</div>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">收货后，超过</div>
					<el-input :disabled="addBtnLoading" class="input" size="small" :min="1"
						v-model.number="form.overdue_day_close_aftersale" type="number"
						oninput="value = parseInt(value) || 1"></el-input>
					<div class="rear_txt">天，关闭售后入口。</div>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">已收货，订单详情页提示语</div>
					<el-input :disabled="addBtnLoading" class="input sales-return" size="medium"
						v-model="form.received_goods_tip" placeholder="请输入" clearable>
					</el-input>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">已完成，订单详情页提示语</div>
					<el-input :disabled="addBtnLoading" class="input sales-return" size="medium"
						v-model="form.completed_tip" placeholder="请输入" clearable>
					</el-input>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">已取消，订单详情页提示语</div>
					<el-input :disabled="addBtnLoading" class="input sales-return" size="medium"
						v-model="form.closed_tip" placeholder="请输入" clearable>
					</el-input>
				</div>
			</el-form-item>
			<h3>退货参数</h3>
			<el-form-item>
				<div class="dflex margin_t_20">
					<div class="front_txt">联系人姓名</div>
					<el-input :disabled="addBtnLoading" class="input sales-return" size="medium"
						v-model="form.sales_return_linkman" placeholder="请输入" clearable>
					</el-input>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">联系人手机号</div>
					<el-input :disabled="addBtnLoading" class="input sales-return" size="medium"
						v-model="form.sales_return_mobile" placeholder="请输入" clearable>
					</el-input>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">售后地址</div>
					<el-cascader :disabled="areaLoading || addBtnLoading" v-loading="areaLoading"
						element-loading-background="#f6f6f6" class="input sales-return-address" size="medium"
						v-model="form.sales_return_address" :options="areaOptions" filterable ref="cascaderObj"
						:props="{ checkStrictly: true, emitPath: false, expandTrigger: 'hover', value: '_id', label: 'name' }"
						clearable></el-cascader>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">售后详细地址</div>
					<el-input :disabled="addBtnLoading" class="input sales-return-address_detail" size="medium"
						v-model="form.sales_return_address_detail" placeholder="请输入" clearable>
					</el-input>
				</div>
			</el-form-item>
			<el-form-item>
				<div class="dflex">
					<div class="front_txt">备注</div>
					<el-input :disabled="addBtnLoading" class="input sales-return-address_detail" type="textarea"
						:autosize="{ minRows: 3 }" v-model="form.sales_return_remark" placeholder="请输入"></el-input>
				</div>
			</el-form-item>
			<el-form-item label-width="210px">
				<use-affix position="bottom" :offset="58">
					<el-button round icon="el-icon-check" :loading="addBtnLoading" class="confirm-btn"
						@click="submitForm('ruleForm')">提交</el-button>
				</use-affix>
			</el-form-item>
		</el-form>
	</use-box>
</template>

<script>
	const __name = 'wmzmall-order-parameter';
	const __area = 'opendb-city-china';
	export default {
		data() {
			return {
				dataId: '',
				formLoading: false,
				areaLoading: false,
				areaOptions: [],
				form: {
					stock_ded_type: 1,
					overdue_minute_cancel_order: 0,
					predict_day_deliver: 0,
					overdue_day_receiving: 0,
					overdue_day_close_evaluated: 0,
					overdue_day_close_aftersale: 0,
					received_goods_tip: '',
					completed_tip: '',
					closed_tip: '',
					sales_return_linkman: '',
					sales_return_mobile: '',
					sales_return_address: '',
					sales_return_address_detail: '',
					sales_return_remark: ''
				},
				addBtnLoading: false
			};
		},
		methods: {
			loadData() {
				this.areaLoading = true;
				this.$db[__area]
				.noaction()
				.noshopid()
				.totree({
					orderby: 'code asc',
					loading: false
				}).then(res => {
					if (res.code == 200) {
						this.setChildren(res.datas);
						this.areaOptions = res.datas;
					}
					this.areaLoading = false;
				});

				this.formLoading = true;

				this.$db[__name].tofirst({
					loading: false
				}).then(res => {
					if (res.code == 200) {
						res.datas = res.datas || {};

						this.dataId = res.datas._id || '';

						for (let item in this.form) {
							this.form[item] = res.datas[item];
						}
					}
					this.formLoading = false;
				});
			},
			submitForm(formName) {
				this.addBtnLoading = true;
				this.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						if (!this.dataId) {
							this.$db[__name].add(this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.addBtnLoading = false;
									return;
								}
								this.loadData();
							}).catch(() => {
								this.addBtnLoading = false;
							})
						} else {
							this.$db[__name].update(this.dataId, this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.addBtnLoading = false;
									return;
								}
								this.loadData();
							}).catch(() => {
								this.addBtnLoading = false;
							})
						}
					}
				});
			},
			setChildren(res) {
				res.forEach(item => {
					if (item.children && item.children.length <= 0) {
						item.children = null;
						return;
					}

					this.setChildren(item.children);
				});
			}
		},
		created() {
			this.loadData();
		},
		mounted() {
			this.$nextTick(() => {
				this.$api.scrollOnStorage('.use-box', this.$route.path);
			});

			this.$nextTick(() => {
				this.$api.scrollToStorage('.use-box', this.$route.path);
			});
		}
	};
</script>

<style>
	>>>.input {
		width: 100px;
		margin: 0 10px;
	}

	>>>.input.sales-return {
		width: 300px;
	}

	>>>.input.sales-return-address {
		width: 400px;
		height: 36px;
	}

	>>>.input.sales-return-address .el-loading-mask {
		border-radius: 4px;
	}

	>>>.input.sales-return-address .el-loading-spinner {
		height: 100%;
		top: 0;
		margin: 0;
	}

	>>>.input.sales-return-address .circular {
		width: 36px;
		height: 36px;
	}

	>>>.input.sales-return-address_detail {
		width: 500px;
	}

	>>>.input .el-input__inner {
		width: 100%;
		background-color: #f6f6f6;
		border-color: #f6f6f6;
		padding: 0 0 0 15px;
	}

	.front_txt {
		width: 200px;
		text-align: right;
		font-size: 13px;
		user-select: none;
	}

	.rear_txt {
		font-size: 13px;
		display: flex;
		align-items: center;
		user-select: none;
	}
</style>
